package com.zhuoyue.biz.core.api.service.impl.device;

import com.zhuoyue.biz.core.api.domain.DeviceAndGatewayDO;
import com.zhuoyue.biz.core.api.domain.TblDeviceActions;
import com.zhuoyue.biz.core.api.dto.req.ControlReq;
import com.zhuoyue.biz.core.api.service.GatewayRedisService;
import com.zhuoyue.biz.core.command.cmd.common.faa.OneColorTemperature0Faa0D03;
import com.zhuoyue.biz.core.command.impl.DanSeWen0Faa0D03;
import com.zhuoyue.biz.core.command.service.CmdSqlHandleService;
import com.zhuoyue.common.utils.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author zhanghui
 */
@Component
public class OneColorTemperatureCommon {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private GatewayRedisService gatewayRedisService;
    @Autowired
    private CmdSqlHandleService cmdSqlHandleService;

    public String oneColorTemperatureDeal(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {
        String cmd = "";
        List<TblDeviceActions> deviceActions2 = cmdSqlHandleService.getActionByDeviceId(deviceAndGatewayDO.getId());
        try {
            cmd = OneColorTemperature0Faa0D03.control(deviceAndGatewayDO, controlReq, deviceActions2, false);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        try {
            String turnOffRequest = "TurnOffRequest";
            if (turnOffRequest.equals(controlReq.getControlType())) {
                Map<String, Object> actions = controlReq.getActions();
                Integer delayTime = actions != null ? (Integer) actions.get("delayTime") : null;
                if (actions == null || delayTime == null || delayTime == 0) {
                    Boolean delete = redisTemplate.delete(deviceAndGatewayDO.getZigbeeMac() + "_delayTime");
                    if (delete) {
                        //发送取消命令
                        String rCmd = DanSeWen0Faa0D03.setCancelDelay(deviceAndGatewayDO.getGatewayMac(), deviceAndGatewayDO.getZigbeeMac(), "0000", "0000", "0000", "0000");
                        gatewayRedisService.writeDataToGateway(deviceAndGatewayDO.getGatewayMac(), rCmd);
                        Thread.sleep(200);
                    }
                } else {
                    Long aLong = Time.toLongMilli(LocalDateTime.now()) + delayTime * 1000;
                    Map<String, Object> dTime = new HashMap<>(2);
                    dTime.put("delayTime", aLong);
                    redisTemplate.opsForValue().set(deviceAndGatewayDO.getZigbeeMac() + "_delayTime", aLong, delayTime, TimeUnit.SECONDS);

                }
            } else {
                Map<String, Object> actions = controlReq.getActions();
                Integer delayTime = (Integer) actions.get("delayTime");
                if (delayTime != null && delayTime > 0) {
                    Long aLong = Time.toLongMilli(LocalDateTime.now()) + delayTime * 1000;
                    redisTemplate.opsForValue().set(deviceAndGatewayDO.getZigbeeMac() + "_delayTime", aLong, delayTime, TimeUnit.SECONDS);
                }
            }
        } catch (Exception e) {
            System.out.printf(e.getMessage());
        }
        return cmd;
    }

}
